'''
$Revision: 124 $
$LastChangedDate: 2009-05-06 14:29:18 +0000 (Wed, 06 May 2009) $
'''

import unittest
import os, sys
from sympy import Symbol, sympify, diff


from MomentClosure.GetMomentEquation import getMomentEquation
from MomentClosure.GetReactionInformation import GetStoichiometryDict
from MomentClosure.VariableDict import Variables

from MomentClosure.GlobalVariables import globalVariables
from MomentClosure.GetTimeDerivatives import getTimeDerivative

class TestMomentEquationsWithDenominators(unittest.TestCase):
    '''Tests the moment closure equations with know cases.
    
        If they work, everything else should as well.
    '''
    def setUp(self):
        pass

    def testMM(self):
        globalVariables(closure_order =5, time_closure_order=5)
        Variables().initialise()

        gdd = GetStoichiometryDict(sbml_file = 'xml_files/test/mm.xml')
        reaction_list = gdd.getReactionList()
        
        cm = Symbol('cm')
        t= Symbol('t')
        t_der = diff(sympify('s(t)'), t)
        t_2der = diff(sympify('E_s_s(t)'), t)
        t_3der = diff(sympify('mu_3(t)'), t)
        t_4der = diff(sympify('mu_4(t)'), t)
        
        mu1 = Variables().getMoment((1,))
        mu2 = Variables().getMoment((2,))
        mu3 = Variables().getMoment((3,)) 
        
        #Moment equation 0
        x=getTimeDerivative((0,), gdd)
        mom_eq = getMomentEquation((0,), reaction_list, gdd.hasDenominator) 
        self.assertEqual(mom_eq, -2*mu1)
        difference = x - (2*cm+1)*t_der-t_2der
        self.assertEqual(difference.expand(), 0)
        
        #Moment equation 1
        x = getTimeDerivative((1,), gdd)
        mom_eq = getMomentEquation((1,), reaction_list, gdd.hasDenominator) 
        difference = mom_eq - (-(cm-1)*mu1-3*mu2)
        self.assertEqual(difference.expand(), 0)
        difference = x - (cm*(cm+1)*t_der+(2*cm+1)*t_2der+t_3der)
        self.assertEqual(difference.expand(), 0)
        
        #Moment equation 2
        x = getTimeDerivative((2,), gdd)
        mom_eq = getMomentEquation((2,), reaction_list, gdd.hasDenominator) 
        difference = mom_eq - ((cm-1)*mu1+(3-2*cm)*mu2-4*mu3)
        self.assertEqual(difference.expand(), 0)
        difference = x - (cm*(cm+1)*t_2der+(2*cm+1)*t_3der+t_4der)
        self.assertEqual(difference.expand(), 0)
        
    def testHill(self):
        globalVariables(closure_order =5, time_closure_order=5)
        Variables().initialise()

        gdd = GetStoichiometryDict(sbml_file = 'xml_files/test/hill.xml')
        reaction_list = gdd.getReactionList()
        
        cm = Symbol('cm')
        t= Symbol('t')
        t_der = diff(sympify('s(t)'), t)
        t_2der = diff(sympify('E_s_s(t)'), t)
        t_3der = diff(sympify('mu_3(t)'), t)
        t_4der = diff(sympify('mu_4(t)'), t)
        t_5der = diff(sympify('mu_5(t)'), t)
        
        mu1 = Variables().getMoment((1,))
        mu2 = Variables().getMoment((2,))
        mu3 = Variables().getMoment((3,)) 
        mu4 = Variables().getMoment((4,))
        #Moment equation 0
        x = getTimeDerivative((0,), gdd)
        mom_eq = getMomentEquation((0,), reaction_list, gdd.hasDenominator) 
        self.assertEqual(mom_eq, -4*mu3)
        difference = x - 2*cm*t_der- (2*cm+1)*t_2der - 2*t_3der-t_4der
        self.assertEqual(difference.expand(), 0)
        
        #Moment equation 1
        x = getTimeDerivative((1,), gdd)
        mom_eq = getMomentEquation((1,), reaction_list, gdd.hasDenominator) 
        difference = mom_eq - (-(cm+1)*mu2+2*mu3-5*mu4)
        self.assertEqual(difference.expand(), 0)
        difference = x - (cm*(cm+1)*t_der+2*cm*t_2der+(2*cm+1)*t_3der+2*t_4der+t_5der)
        self.assertEqual(difference.expand(), 0)
        
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
               
    
if __name__ == '__main__':
    unittest.main()


